<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'proj:zg:pmRectification:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    :showOkBtn="false"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjZgPmRectificationForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    PmRectification,
    pmRectificationSave,
    pmRectificationForm,
  } from '/@/api/proj/zg/pmRectification';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('proj.zg.pmRectification');
  const { showMessage } = useMessage();
  const record = ref<PmRectification>({} as PmRectification);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('整改信息') : t('整改信息'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('整改信息'),
      field: 'checkInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('工程名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('工程编号'),
      field: 'projectno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('整改期限'),
      field: 'endtime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      // required: true,
      dynamicDisabled: true,
    },
    {
      label: t('整改内容'),
      field: 'rectificationcontent',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      dynamicDisabled: true,
    },
    {
      label: t('整改措施'),
      field: 'rectificationmeasures',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      // required: true,
      dynamicDisabled: true,
    },
    {
      label: t('整改图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'rectList_file',
        uploadType: 'all',
      },
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['rectList_file__len'] || 0;
      //         if (len == 0) reject(t('请上传图片'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
    {
      label: t('整改结果'),
      field: 'acceptInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('整改结果'),
      field: 'rectificationresult',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('整改时间'),
      field: 'rectificationdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
      },
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('完成图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'pmRectification_image',
        uploadType: 'image',
      },
      rules: [
        { required: true },
        {
          validator(_rule, value) {
            return new Promise((resolve, reject) => {
              const len = !value || value['pmRectification_image__len'] || 0;
              if (len == 0) reject(t('请上传图片'));
              else resolve();
            });
          },
        },
      ],
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
    {
      label: t('验收信息'),
      field: 'acceptInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('验收结果'),
      field: 'checkresult',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'zf_check_result',
      },
      required: true,
      dynamicDisabled: true,
      // colProps: { lg: 24, md: 24 },
    },
    {
      label: t('验收时间'),
      field: 'checkdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
      },
      required: true,
      dynamicDisabled: true,
    },
    {
      label: t('验收意见'),
      field: 'checkopinion',
      component: 'InputTextArea',
      componentProps: {
        maxlength: -1,
        rows: 5,
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmRectificationForm(data);
    record.value = (res.pmRectification || {}) as PmRectification;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmRectificationSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
